package com.gogowan.agroenterprise.storage;

import java.util.ArrayList;
import java.util.List;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.barcode.Intents;
import com.gogowan.agroenterprise.base.TitleActivity;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask.AsyncLoadListener;
import com.gogowan.agroenterprise.contorl.WeakRefAsyncTask.InterActionModel;
import com.gogowan.agroenterprise.model.BindCodeInfoRequest;
import com.gogowan.agroenterprise.model.BoxBindCodeRequest;
import com.gogowan.agroenterprise.model.BoxBindCodeResult;
import com.gogowan.agroenterprise.model.CodeInfoResult;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.MaSingleCodeRequest;
import com.gogowan.agroenterprise.model.MaSingleCodeResult;
import com.gogowan.agroenterprise.model.MaterialCodeRequest;
import com.gogowan.agroenterprise.model.MaterialCodeResult;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.model.ResultInfo;
import com.gogowan.agroenterprise.model.SplitBoxGetCodeRequest;
import com.gogowan.agroenterprise.model.SplitBoxGetCodeResult;
import com.gogowan.agroenterprise.newview.HWDialogActivity;
import com.gogowan.agroenterprise.utils.PalUtils;
import com.gogowan.agroenterprise.utils.SystemBroadCast;
import com.gogowan.agroenterprise.utils.Utils;
import com.gogowan.agroenterprise.widget.adapter.PackAdapter;
import com.ioid.utils.JsonUtils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @Description:装箱装盒
 * @author mdp
 * @date 2017年1月6日 上午9:18:06
 */
public class PackActivity extends TitleActivity implements AsyncLoadListener {

	/** 1 box 2 gift */
	private int flag;
	private LoginResultInfo loginResult;
	private BoxBindCodeResult boxBindCodeResult;
	private List<CodeInfoResult> codeInfoList = new ArrayList<>();
	private ArrayList<String> codeList = new ArrayList<String>();
	private PackAdapter packAdapter;
	private WeakRefAsyncTask weakRefAsyncTask = new WeakRefAsyncTask(this);
	private static final int SERIAL_RQT = 1;
	private static final int STEP_RQT = 2;
	private static final int COMMIT_RQT = 3;
	private static final int SCAN_SERIAL_RQC = 1;
	private static final int SCAN_BEGIN_RQC = 2;
	private static final int SCAN_END_RQC = 3;
	private static final int SCAN_SERIAL_RQC_HW = 100;
	private static final int SCAN_BE_RQC_HW = 200;
	private static final int UNPACK_RQT = 6;
	private String scanCode;
	private String beginCode, endCode;
	private Button pack_lxsm_btn, pack_smmd_btn;
	private Button pack_lxsm_btn_hw, pack_smmd_btn_hw;
	// 箱码显示控件
	private EditText packCodeTV;
	// 扫描箱码 和盒码
	private ImageView cribSmhqdhBtn;
	private static final int BOX_RQT = 4;
	private static final int GIFT_RQT = 5;

	private static final int BOX_RQC = 199301;
	private static final int GIFT_RQC = 199302;
	private String resultInfoStr = "二维码错误，请扫描正确二维码";
	private ImageView rightIV;
	/// 所有码类型（1：产品码；2：盒码）当前返回的类型
	public int allCodeType;
	private TextView SC_Tv;
	private boolean RedScanOrder=false;


	@Override
	protected int setContentViewID() {
		return R.layout.activity_pack;
	}

	@Override
	protected boolean initData(Intent intent, Bundle savedInstanceState) {
		if (savedInstanceState != null) {
			beginCode = savedInstanceState.getString("beginCode");
		}
		loginResult = PalUtils.getLoginResultInfo(getApplicationContext());
		// scanCode = intent.getStringExtra(PalUtils.PACK_CODE_ITT);
		// 1:代表装箱 2：代表装盒
		flag = intent.getIntExtra(PalUtils.PACK_TYPE_ITT, 0);
		// boxBindCodeResult = (BoxBindCodeResult) intent
		// .getSerializableExtra(PalUtils.PACK_MODEL_ITT);
		return flag != 0 && loginResult != null;
	}

	@Override
	protected void initView() {
		initTitle(flag == 1 ? "装箱操作" : "装盒操作", true);
		((TextView) findViewById(R.id.pack_xmnr_tv0)).setText(flag == 1 ? "箱码:" : "盒码:");
		packCodeTV = (EditText) findViewById(R.id.pack_xmnr_tv);
		rightIV = (ImageView) findViewById(R.id.all_top_right_im);
		SC_Tv=(TextView)findViewById(R.id.scancount_tv);
		rightIV.setOnClickListener(this);
		findViewById(R.id.orderll).setOnClickListener(this);
		// ((TextView)
		// findViewById(R.id.pack_xmnr_tv)).setText(boxBindCodeResult
		// .getBoxCode());
		// ((TextView)
		// findViewById(R.id.pack_cpmc_tv)).setText(boxBindCodeResult
		// .getMaterianName());
		if (flag == 1) {
			resultInfoStr = "请先扫描箱码";
		} else if (flag == 2) {
			resultInfoStr = "请先扫描盒码";
		}
		cribSmhqdhBtn = (ImageView) findViewById(R.id.crib_smhqdh_btn);
		cribSmhqdhBtn.setOnClickListener(this);
		packAdapter = new PackAdapter(this, codeInfoList, R.layout.layout_pack_item);
		ListView listView = (ListView) findViewById(R.id.pack_lv);
		listView.setAdapter(packAdapter);
		SC_Tv.setText(codeInfoList.size()+"个码");
		pack_lxsm_btn = (Button) findViewById(R.id.pack_lxsm_btn);
		pack_lxsm_btn.setOnClickListener(this);
		pack_smmd_btn = (Button) findViewById(R.id.pack_smmd_btn);
		pack_smmd_btn.setOnClickListener(this);
		pack_lxsm_btn_hw = (Button) findViewById(R.id.pack_lxsm_btn_hw);
		pack_lxsm_btn_hw.setOnClickListener(this);
		pack_smmd_btn_hw = (Button) findViewById(R.id.pack_smmd_btn_hw);
		pack_smmd_btn_hw.setOnClickListener(this);
		findViewById(R.id.pack_commit_btn).setOnClickListener(this);
		initScanView(new SystemBroadCast.ScanResultListener() {
			@Override
			public void OnResult(String CodeString) {
				if (isVisible){
					xiaobangScan(CodeString);
				}
			}
		});
	}

	private void xiaobangScan(String resultStr) {
		// 扫描后的码内容
		scanCode = Utils.getCodeStr(resultStr.trim());
		if (flag == 1) {
			// 装箱
			requestPackScanCode(1, scanCode);
		} else if (flag == 2) {
			// 装盒
			boolean isHave = false;
			for (CodeInfoResult c : codeInfoList) {
				if (Utils.getTail_Code(scanCode).equals(Utils.getTail_Code(c.getCode()))) {
					isHave = true;
					break;
				}
			}
			if (isHave) {
				Toast.makeText(getApplicationContext(), "码号重复", Toast.LENGTH_SHORT).show();
			} else {
				requestPackScanCode(2, scanCode);
			}
		}
	}

	@Override
	protected void onClicked(View v) {
		Intent intent;
		switch (v.getId()) {
			case R.id.orderll:
				RedScanOrder=true;
				findViewById(R.id.orderll).setBackgroundResource(R.color.color_4002637B);
					break;
			// 连续扫码
			case R.id.pack_lxsm_btn:
				if (boxBindCodeResult == null) {
					PalUtils.showShortToast(getApplicationContext(), resultInfoStr);
					return;
				}
				// 背景修改
				/*pack_lxsm_btn.setBackgroundResource(R.drawable.btn_pre_bg1);
				pack_lxsm_btn.setTextColor(getResources().getColor(R.color.white));

				pack_smmd_btn.setBackgroundResource(R.drawable.scanout_btn_selector);
				pack_smmd_btn.setTextColor(getResources().getColor(R.color.text_color_0dcc6c));*/
				intent = new Intent(Intents.Scan.ACTION);
				intent.putExtra(Intents.Scan.IS_BULT_MODE, true);
				intent.putExtra("emptyCode", boxBindCodeResult.getBoxCode());
				intent.putExtra("emptyCode1", scanCode);
				this.startActivityForResult(intent, SCAN_SERIAL_RQC);
				break;
			// 扫描码段 -- 起始码
			case R.id.pack_smmd_btn:
				if (boxBindCodeResult == null) {
					PalUtils.showShortToast(getApplicationContext(), resultInfoStr);
					return;
				}
				/*pack_smmd_btn.setBackgroundResource(R.drawable.btn_pre_bg1);
				pack_smmd_btn.setTextColor(getResources().getColor(R.color.white));
				pack_lxsm_btn.setBackgroundResource(R.drawable.scanout_btn_selector);
				pack_lxsm_btn.setTextColor(getResources().getColor(R.color.text_color_0dcc6c));*/
				intent = new Intent(Intents.Scan.ACTION);
				intent.putExtra("emptyCode", boxBindCodeResult.getBoxCode());
				intent.putExtra("emptyCode1", scanCode);
				this.startActivityForResult(intent, SCAN_BEGIN_RQC);
				break;
			// 确认关联
			case R.id.pack_commit_btn:
				new AlertDialog.Builder(PackActivity.this)
						.setTitle("提示")
						.setMessage("您是否确定要提交数据？")
						.setNegativeButton("确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								if (boxBindCodeResult == null) {
									PalUtils.showShortToast(getApplicationContext(), resultInfoStr);
									return;
								}
								commit();
							}
						})
						.setNeutralButton("取消", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {

							}
						}).show();
				break;
			case R.id.crib_smhqdh_btn:
				if (flag == 1) {
					// 装箱
					Intent intent1 = new Intent(Intents.Scan.ACTION);
					startActivityForResult(intent1, BOX_RQC);
				} else if (flag == 2) {
					// 装盒
					Intent intent2 = new Intent(Intents.Scan.ACTION);
					startActivityForResult(intent2, GIFT_RQC);
				}
				break;
			case R.id.all_top_right_im:
				// 查询历史记录
				Intent intents = new Intent(PackActivity.this, HistoryActivity.class);
				intents.putExtra("flag", flag);
				startActivity(intents);
				break;
			case R.id.pack_lxsm_btn_hw:
				if (boxBindCodeResult == null) {
					PalUtils.showShortToast(getApplicationContext(), resultInfoStr);
					return;
				}
				intent = new Intent(PackActivity.this, HWDialogActivity.class);
				intent.putExtra("emptyCode", boxBindCodeResult.getBoxCode());
				intent.putExtra("emptyCode1", scanCode);
				intent.putExtra("type", "lx");
				this.startActivityForResult(intent, SCAN_SERIAL_RQC_HW);
				break;
			case R.id.pack_smmd_btn_hw:
				if (boxBindCodeResult == null) {
					PalUtils.showShortToast(getApplicationContext(), resultInfoStr);
					return;
				}
				intent = new Intent(PackActivity.this, HWDialogActivity.class);
				intent.putExtra("emptyCode", boxBindCodeResult.getBoxCode());
				intent.putExtra("emptyCode1", scanCode);
				intent.putExtra("type", "md");
				this.startActivityForResult(intent, SCAN_BE_RQC_HW);
				break;
		}
	}

	@Override
	public void handleMessage(TitleActivity activity, android.os.Message msg) {
		switch (msg.what) {
		case 0:
			packAdapter.notifyDataSetChanged();
			SC_Tv.setText(codeInfoList.size()+"个码");
			break;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		weakRefAsyncTask.cancel();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("beginCode", beginCode);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			// 连续扫码
			case SCAN_SERIAL_RQC:
				ArrayList<String> resultStringList = intent.getStringArrayListExtra(Intents.Scan.RESULTLIST);
				if (resultStringList == null || resultStringList.size() == 0) {
					return;
				}
				showProgressDialog(getString(R.string.alert_message));
				if (Utils.isEmpty(scanCode)){
					if (flag==1){
						showToast("请扫描箱码！");
					}else {
						showToast("请扫描盒码！");
					}
					return;
				}
				MaSingleCodeRequest maSingleCodeRequest = new MaSingleCodeRequest();
				maSingleCodeRequest.setBoxCode(scanCode);
				//maSingleCodeRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
				maSingleCodeRequest.setListCode(resultStringList);
				String json = Utils.toJSON(maSingleCodeRequest);
				Message message = new Message(
						flag == 1 ? PalUtils.MsgType.ScanMaSingleCode : PalUtils.MsgType.ScanPackMaSingleCode,
						PalUtils.ENCRYPT, json);
				InterActionModel interActionModel = new InterActionModel();
				interActionModel.setIndex(SERIAL_RQT);
				interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
				interActionModel.setMessage(message);
				weakRefAsyncTask.execute(interActionModel);
				break;
			// 扫描码段 -- 起始码
			case SCAN_BEGIN_RQC:
				beginCode = Utils.getCodeStr(intent.getStringExtra(Intents.Scan.RESULT));
				intent = new Intent(Intents.Scan.ACTION);
				intent.putExtra("emptyCode", boxBindCodeResult.getBoxCode());
				intent.putExtra("emptyCode1", scanCode);
				this.startActivityForResult(intent, SCAN_END_RQC);
				break;
			// 扫描码段 -- 结束码
			case SCAN_END_RQC:
				endCode = Utils.getCodeStr(intent.getStringExtra(Intents.Scan.RESULT));
				requestStepList();
				break;
			case BOX_RQC:
				// 扫描后的码内容
				scanCode = Utils.getCodeStr(intent.getStringExtra(Intents.Scan.RESULT).trim());
				requestPackScanCode(1, scanCode);
				break;
			// 盒码
			case GIFT_RQC:
				scanCode = Utils.getCodeStr(intent.getStringExtra(Intents.Scan.RESULT).trim());
				boolean isHave = false;
				for (CodeInfoResult c : codeInfoList) {
					if (scanCode.equals(c.getCode())) {
						isHave = true;
						break;
					}
				}
				if (isHave) {
					Toast.makeText(getApplicationContext(), "码号重复", Toast.LENGTH_SHORT).show();
				} else {
					requestPackScanCode(2, scanCode);
				}
				break;

			case SCAN_SERIAL_RQC_HW:
				ArrayList<String> results = intent.getStringArrayListExtra(Intents.Scan.RESULTLIST);
				if (results == null || results.size() == 0) {
					return;
				}
				showProgressDialog(getString(R.string.alert_message));
				MaSingleCodeRequest codeRequest = new MaSingleCodeRequest();
				codeRequest.setBoxCode(boxBindCodeResult.getBoxCode());
				//codeRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
				codeRequest.setListCode(results);
				String jsons = Utils.toJSON(codeRequest);
				Message messages = new Message(
						flag == 1 ? PalUtils.MsgType.ScanMaSingleCode : PalUtils.MsgType.ScanPackMaSingleCode,
						PalUtils.ENCRYPT, jsons);
				InterActionModel interActionModel1 = new InterActionModel();
				interActionModel1.setIndex(SERIAL_RQT);
				interActionModel1.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
				interActionModel1.setMessage(messages);
				weakRefAsyncTask.execute(interActionModel1);
				break;
			case SCAN_BE_RQC_HW:
				ArrayList<String> result = intent.getStringArrayListExtra(Intents.Scan.RESULTLIST);
				if (result == null || result.size() == 0) {
					return;
				}
				beginCode = result.get(0);
				endCode = result.get(1);
				requestStepList();
				break;
			}
		}
	}

	@Override
	public void onResult(AsyncLoadListener asyncLoadListener, String response, int actionId) {
		hideProgressDialog();
		if (isAlreadyDestroyed()) {
			return;
		}
		if (PalUtils.isEmpty(response)) {
			PalUtils.showShortToast(getApplicationContext(), getString(R.string.server_error));
			if (actionId == BOX_RQT || actionId == GIFT_RQT) {
				boxBindCodeResult = null;
				packCodeTV.setText("");
			}
		} else {
			switch (actionId) {
			// 连续扫码产品码
			case SERIAL_RQT:
				MaSingleCodeResult maSingleCodeResult = (MaSingleCodeResult) PalUtils.getPostResult(response,
						MaSingleCodeResult.class);
				if (maSingleCodeResult == null) {
					PalUtils.showShortToast(getApplicationContext(), getString(R.string.analyse_error));
				} else if (maSingleCodeResult.getResult() > 0) {
					if (maSingleCodeResult.getListCode() != null && maSingleCodeResult.getListCode().size() > 0) {
						if (flag == 1) {
							// 装箱
							if (allCodeType == 0) {
								allCodeType = maSingleCodeResult.getAllCodeType();
							} else {
								// 判断码类型是否相同
								if (allCodeType != maSingleCodeResult.getAllCodeType()) {
									PalUtils.showShortToast(getApplicationContext(),
											getResources().getString(R.string.type_msg_default_status));
									return;
								}
							}
						}
						PalUtils.showShortToast(getApplicationContext(), maSingleCodeResult.getResultinfo());
						for (CodeInfoResult c1 : maSingleCodeResult.getListCode()) {
							boolean isHave = false;
							for (CodeInfoResult c : codeInfoList) {
								if (c1.getCode().equals(c.getCode())) {
									isHave = true;
									break;
								}
							}
							if (!isHave) {
								codeInfoList.add(c1);
								codeList.add(c1.getCode());
							}

						}
					}
					// codeInfoList.clear();
					// codeInfoList.addAll(maSingleCodeResult.getListCode());
					getWeakRefHandler().sendEmptyMessage(0);
				} else {
					PalUtils.showShortToast(getApplicationContext(), maSingleCodeResult.getResultinfo());
				}
				break;
			// 分段扫码产品码
			case STEP_RQT:
				MaterialCodeResult materialCodeResult = (MaterialCodeResult) PalUtils.getPostResult(response,
						MaterialCodeResult.class);
				if (materialCodeResult == null) {
					PalUtils.showShortToast(getApplicationContext(), getString(R.string.analyse_error));
				} else if (materialCodeResult.getResult() > 0) {
					if (materialCodeResult.getLiCodeInfo() != null && materialCodeResult.getLiCodeInfo().size() > 0) {
						if (flag == 1) {
							// 装箱
							if (allCodeType == 0) {
								allCodeType = materialCodeResult.getAllCodeType();
							} else {
								// 判断码类型是否相同
								if (allCodeType != materialCodeResult.getAllCodeType()) {
									PalUtils.showShortToast(getApplicationContext(),
											getResources().getString(R.string.type_msg_default_status));
									return;
								}
							}
						}
						PalUtils.showShortToast(getApplicationContext(), materialCodeResult.getResultinfo());
						for (CodeInfoResult c1 : materialCodeResult.getLiCodeInfo()) {
							boolean isHave = false;
							for (CodeInfoResult c : codeInfoList) {
								if (c1.getCode().equals(c.getCode())) {
									isHave = true;
									break;
								}
							}
							if (!isHave) {
								codeInfoList.add(c1);
								codeList.add(c1.getCode());
							}
						}
					}
					// codeInfoList.clear();
					// codeInfoList.addAll(materialCodeResult.getLiCodeInfo());
					getWeakRefHandler().sendEmptyMessage(0);
				} else {
					PalUtils.showShortToast(getApplicationContext(), materialCodeResult.getResultinfo());
				}
				break;
			// 提交
			case COMMIT_RQT:
				ResultInfo resultInfo = (ResultInfo) PalUtils.getPostResult(response, ResultInfo.class);
				if (resultInfo == null) {
					PalUtils.showShortToast(getApplicationContext(), getString(R.string.analyse_error));
				} else if (resultInfo.getResult() > 0) {
					PalUtils.showShortToast(getApplicationContext(), resultInfo.getResultinfo());
					finish();
				} else {
					PalUtils.showShortToast(getApplicationContext(), resultInfo.getResultinfo());
				}
				break;
			case BOX_RQT:
				final BoxBindCodeResult boxBindCodeResult2 = (BoxBindCodeResult) PalUtils.getPostResult(response,
						BoxBindCodeResult.class);
				if (boxBindCodeResult2 == null) {
					PalUtils.showShortToast(getApplicationContext(), getString(R.string.analyse_error));
					boxBindCodeResult = null;
					packCodeTV.setText("");
				} else if (boxBindCodeResult2.getResult() > 0) {
					// Intent intent = new Intent(this, PackActivity.class);
					// intent.putExtra(PalUtils.PACK_MODEL_ITT,
					// boxBindCodeResult);
					// intent.putExtra(PalUtils.PACK_TYPE_ITT, 1);
					// intent.putExtra(PalUtils.PACK_CODE_ITT, scanCode);
					// startActivity(intent);
					packCodeTV.setText(boxBindCodeResult2.getBoxCode());
					boxBindCodeResult = boxBindCodeResult2;
				} else {
					packCodeTV.setText("");
					if (boxBindCodeResult2.getBoxStatus() == 2) {
						// 已经装过箱提示是否拆箱
						ShowDialog1("提示", "该码已经完成装箱，是否要进行拆箱操作", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								// 确定之后跳转拆箱页面
								requestUnpackScanCode(boxBindCodeResult2.getBoxCode());
							}
						}, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								dialog.dismiss();
							}
						});
					} else {
						PalUtils.showShortToast(getApplicationContext(), boxBindCodeResult2.getResultinfo());
					}
					// packCodeTV.setText(scanCode);
					boxBindCodeResult = null;
				}
				break;
			// 盒码
			case GIFT_RQT:
				final BoxBindCodeResult boxBindCodeResult3 = (BoxBindCodeResult) PalUtils.getPostResult(response,
						BoxBindCodeResult.class);
				if (boxBindCodeResult3 == null) {
					PalUtils.showShortToast(getApplicationContext(), getString(R.string.analyse_error));
					boxBindCodeResult = null;
					packCodeTV.setText("");
				} else if (boxBindCodeResult3.getResult() > 0) {
					// Intent intent = new Intent(this, PackActivity.class);
					// intent.putExtra(PalUtils.PACK_MODEL_ITT,
					// boxBindCodeResult1);
					// intent.putExtra(PalUtils.PACK_TYPE_ITT, 2);
					// intent.putExtra(PalUtils.PACK_CODE_ITT, scanCode);
					// startActivity(intent);
					packCodeTV.setText(boxBindCodeResult3.getBoxCode());
					boxBindCodeResult = boxBindCodeResult3;
				} else {
					if (boxBindCodeResult3.getPackStatus() == 2) {
						// 已经装过箱提示是否拆箱
						ShowDialog1("提示", "该码已经完成装盒，是否要进行拆盒操作", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								// 确定之后跳转拆箱页面
								requestUnpackScanCode(boxBindCodeResult3.getBoxCode());
							}
						}, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								dialog.dismiss();
							}

						});
					} else {
						PalUtils.showShortToast(getApplicationContext(), boxBindCodeResult3.getResultinfo());
					}
					boxBindCodeResult = null;
					packCodeTV.setText("");
					// packCodeTV.setText(scanCode);
					// resultInfoStr = boxBindCodeResult3.getResultinfo();
				}
				break;
			// 拆箱拆盒
			case UNPACK_RQT:
				SplitBoxGetCodeResult bindCodeRemoveResult = (SplitBoxGetCodeResult) PalUtils.getPostResult(response,
						SplitBoxGetCodeResult.class);
				if (bindCodeRemoveResult == null) {
					PalUtils.showShortToast(getApplicationContext(), getString(R.string.analyse_error));
				} else if (bindCodeRemoveResult.getResult() > 0) {
					Intent intent = new Intent(this, UnpackActivity.class);
					intent.putExtra(PalUtils.PACK_MODEL_ITT, bindCodeRemoveResult);
					startActivity(intent);
				} else {
					PalUtils.showShortToast(getApplicationContext(), bindCodeRemoveResult.getResultinfo());
				}
				break;
			}
		}
	}

	/**
	 * 获取分段扫码列表数据
	 */
	private void requestStepList() {
		if (PalUtils.isEmpty(Utils.getCodeStr(beginCode)) || PalUtils.isEmpty(Utils.getCodeStr(endCode))) {
			PalUtils.showShortToast(getApplicationContext(), getString(R.string.sales_msg_default_status));
			return;
		}
		showProgressDialog(getString(R.string.alert_message));
		MaterialCodeRequest materialCodeRequest = new MaterialCodeRequest();
		//materialCodeRequest.setEnterpriseID("" + loginResult.getEnterpriseinfo().getEnterpriseid());
		materialCodeRequest.setStartCode(beginCode);
		materialCodeRequest.setEndCode(endCode);
		materialCodeRequest.setBoxCode(boxBindCodeResult.getBoxCode());
		materialCodeRequest.setIsnew(true);
		String json = Utils.toJSON(materialCodeRequest);
		Message message = new Message(flag == 1 ? PalUtils.MsgType.ScanMaCode : PalUtils.MsgType.ScanPackMaCode,
				PalUtils.ENCRYPT, json);
		InterActionModel interActionModel = new InterActionModel();
		interActionModel.setIndex(STEP_RQT);
		interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
		interActionModel.setMessage(message);
		weakRefAsyncTask.execute(interActionModel);
	}

	/**
	 * 提交
	 */
	private void commit() {
		if (codeInfoList == null || codeInfoList.size() == 0) {
			PalUtils.showShortToast(getApplicationContext(), getString(R.string.sales_msg_default_status));
			return;
		}
		showProgressDialog(getString(R.string.committing));
		List<String> listCode = new ArrayList<>();
		for (CodeInfoResult codeInfoResult : codeInfoList) {
			listCode.add(codeInfoResult.getCode());
		}
		BindCodeInfoRequest bindCodeInfoRequest = new BindCodeInfoRequest();
		//bindCodeInfoRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
		//bindCodeInfoRequest.setUserId(loginResult.getUserinfo().getPersonid());
		bindCodeInfoRequest.setBoxCode(boxBindCodeResult.getBoxCode());
		bindCodeInfoRequest.setListCode(listCode);
		String json = Utils.toJSON(bindCodeInfoRequest);
		Message message = new Message(flag == 1 ? PalUtils.MsgType.AddBoxCode : PalUtils.MsgType.AddPackCode,
				PalUtils.ENCRYPT, json);
		InterActionModel interActionModel = new InterActionModel();
		interActionModel.setIndex(COMMIT_RQT);
		interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
		interActionModel.setMessage(message);
		weakRefAsyncTask.execute(interActionModel);
	}

	/**
	 * 获取扫码数据-装箱装盒
	 * 
	 * @param flag
	 *            1 box 2 gift 3扫描垛位码信息
	 */
	private void requestPackScanCode(int flag, String scanCode) {
		if (PalUtils.isEmpty(scanCode)) {
			if (flag == 1) {
				PalUtils.showShortToast(getApplicationContext(), getString(R.string.pack_msg_default_status));
			} else if (flag == 2) {
				PalUtils.showShortToast(getApplicationContext(), getString(R.string.sales_msg_default_status));
			}
			return;
		}
		showProgressDialog(getString(R.string.alert_message));
		BoxBindCodeRequest boxBindCodeRequest = new BoxBindCodeRequest();
		//boxBindCodeRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
		boxBindCodeRequest.setBoxCode(scanCode);
		String json = Utils.toJSON(boxBindCodeRequest);
		int magType = PalUtils.MsgType.BoxCode;
		InterActionModel interActionModel = new InterActionModel();
		switch (flag) {
		case 1:
			magType = PalUtils.MsgType.BoxCode;
			interActionModel.setIndex(BOX_RQT);
			break;
		case 2:
			interActionModel.setIndex(GIFT_RQT);
			magType = PalUtils.MsgType.PackCode;
		}
		Message message = new Message(magType, PalUtils.ENCRYPT, json);
		interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
		interActionModel.setMessage(message);
		String ss=  Utils.toJSON(message);
		weakRefAsyncTask.execute(interActionModel);
	}

	/**
	 * 获取扫码数据-拆箱拆盒
	 * 
	 *
	 *             1 box 2 gift
	 */
	private void requestUnpackScanCode(String scanCode) {
		if (PalUtils.isEmpty(scanCode)) {
			PalUtils.showShortToast(getApplicationContext(), getString(R.string.sales_msg_default_status));
			return;
		}
		showProgressDialog(getString(R.string.alert_message));
		SplitBoxGetCodeRequest boxBindCodeRequest = new SplitBoxGetCodeRequest();
		//boxBindCodeRequest.setEnterpriseId(loginResult.getEnterpriseinfo().getEnterpriseid());
		boxBindCodeRequest.setCode(scanCode);
		String json = Utils.toJSON(boxBindCodeRequest);
		Message message = new Message(PalUtils.MsgType.SplitBoxGetCode, PalUtils.ENCRYPT, json);
		InterActionModel interActionModel = new InterActionModel();
		interActionModel.setIndex(UNPACK_RQT);
		interActionModel.setUrl(getString(R.string.first_url) + getString(R.string.first_url_page));
		interActionModel.setMessage(message);
		weakRefAsyncTask.execute(interActionModel);
	}

	@Override
	public void onResume() {
		isVisible = true;
		super.onResume();
		//registerReceiver(new String[] { getString(R.string.SCAN_ACTION) });
		registerIDataReceiver(new String[] { getString(R.string.SCAN_IDATA_ACTION) });
	}

	@Override
	protected void onPause() {
		isVisible = false;
		super.onPause();
	}

	@Override
	protected void handleReceiver(Context context, Intent intent) {
		super.handleReceiver(context, intent);
		if (isVisible) {
			if (getString(R.string.SCAN_ACTION).equals(intent.getAction())) {
				String scanStatus = intent.getStringExtra(getString(R.string.SCAN_STATE));
				if ("ok".equals(scanStatus)) {
					// 成功
					String resultStr = intent.getStringExtra(getString(R.string.DATA_STRING)).trim();
					// 扫描后的码内容
					scanCode = Utils.getCodeStr(resultStr.trim());
					if (flag == 1) {
						// 装箱
						requestPackScanCode(1, scanCode);
					} else if (flag == 2) {
						// 装盒
						boolean isHave = false;
						for (CodeInfoResult c : codeInfoList) {
							if (scanCode.equals(c.getCode())) {
								isHave = true;
								break;
							}
						}
						if (isHave) {
							Toast.makeText(getApplicationContext(), "码号重复", Toast.LENGTH_SHORT).show();
						} else {
							requestPackScanCode(2, scanCode);
						}
					}
				} else {
					// 失败如超时等
					showToast(getString(R.string.scan_timeout_warn), 0);
				}
			}
		}
	}
	
	@Override
	protected void handleIDataReceiver(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.handleIDataReceiver(context, intent);
		if (isVisible) {
			if (getString(R.string.SCAN_IDATA_ACTION).equals(intent.getAction())||
				getString(R.string.SCAN_ACTION).equals(intent.getAction())) {
				String scanResult = intent.getStringExtra("value")+"";
				if (scanResult.length() > 0) {
					// 扫描后的码内容
					scanCode = Utils.getCodeStr(scanResult.trim());
					if (RedScanOrder){
						RedScanOrder=false;
						packCodeTV.setText(scanCode);
						findViewById(R.id.orderll).setBackgroundResource(R.color.font_white);
						return;
					}
					if (flag == 1) {
						// 装箱
						requestPackScanCode(1, scanCode);
					} else if (flag == 2) {
						// 装盒
						boolean isHave = false;
						for (CodeInfoResult c : codeInfoList) {
							if (scanCode.equals(c.getCode())) {
								isHave = true;
								break;
							}
						}
						if (isHave) {
							Toast.makeText(getApplicationContext(), "码号重复", Toast.LENGTH_SHORT).show();
						} else {
							requestPackScanCode(2, scanCode);
						}
					}
				} else {
					// 失败如超时等
					showToast(getString(R.string.scan_timeout_warn), 0);
				}
			}
		}
	}
}
